<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="${entity}Map" type="${package.Entity}.${entity}">
<#list table.fields as field>
    <#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
    </#if>
</#list>
    <#list table.commonFields as field><#--生成公共字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
    </#list>
<#list table.fields as field>
    <#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
    </#if>
</#list>
    </resultMap>

</#if>
<#if baseColumnList>
    <!-- 通用查询结果列 -->
    <sql id="${entity}_Column_List">
       <#list table.commonFields as field>a.${field.name},</#list><#list table.fields as field>a.${field.name}<#if field_has_next>,</#if></#list>
    </sql>
</#if>
    <sql id="Where_${entity}">
        <#noparse>${</#noparse>ew.customSqlSegment<#noparse>}</#noparse>
    </sql>

    <!-- 添加${cfg.controllerName} -->
    <insert id="save" parameterType="${package.Entity}.${entity}">
        INSERT INTO
        ${table.name}
        (<#list table.fields as field>${field.name}<#if field_has_next>,</#if></#list><#list table.commonFields as field><#if field.name != "delete_time" && field.name != "delete_by" && field.name != "delete_name" && field.name != "update_time" && field.name != "update_by" && field.name != "update_name">,${field.name}</#if></#list>)
        VALUES
        (<#list table.fields as field><#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse>,</#list><#list table.commonFields as field><#if field.propertyName != "deleteTime" && field.propertyName != "deleteBy" && field.name != "delete_name" && field.name != "update_time" && field.name != "update_by" && field.name != "update_name"><#if field_index != 0>,</#if><#noparse>#{</#noparse>${field.propertyName}<#noparse>}</#noparse></#if></#list>)
    </insert>
<#list table.fields as field>
<#if field_index == 0 && field.customMap.engine != "MRG_MYISAM">

    <!-- 添加${cfg.controllerName}批量 -->
    <insert id="saveBatch">
        INSERT INTO
        ${table.name}
        (<#list table.fields as field>${field.name}<#if field_has_next>,</#if></#list><#list table.commonFields as field><#if field.name != "delete_time" && field.name != "delete_by" && field.name != "delete_name" && field.name != "update_time" && field.name != "update_by" && field.name != "update_name">,${field.name}</#if></#list>)
        VALUES
        <foreach collection="list" item="item" index="index" separator=",">
            (<#list table.fields as field><#noparse>#{item.</#noparse>${field.propertyName}<#noparse>}</#noparse>,</#list><#list table.commonFields as field><#if field.propertyName != "deleteTime" && field.propertyName != "deleteBy" && field.name != "delete_name" && field.name != "update_time" && field.name != "update_by" && field.name != "update_name"><#if field_index != 0>,</#if><#noparse>#{item.</#noparse>${field.propertyName}<#noparse>}</#noparse></#if></#list>)
        </foreach>
    </insert>
</#if>
</#list>

    <!-- 查询${cfg.controllerName} -->
    <select id="select" resultMap="${entity}Map">
        SELECT
        <include refid="${entity}_Column_List"></include>
        FROM
        <include refid="select_sql"></include>
    </select>

    <!-- 查询${cfg.controllerName}数量 -->
    <select id="selectCount" resultType="int">
        SELECT
        count(1)
        FROM
        <include refid="select_sql"></include>
    </select>

    <sql id="select_sql">
        ${table.name} a
        <if test="ew != null and ew.customSqlSegment != null">
            <include refid="Where_${entity}"></include>
        </if>
    </sql>

    <!-- [通过id查询${cfg.controllerName}] -->
    <select id="selectById" resultMap="${entity}Map">
        SELECT
        <include refid="${entity}_Column_List"></include>
        FROM
        ${table.name} a
        where
        a.id = <#noparse>#{id}</#noparse>
    </select>

    <!-- [修改${cfg.controllerName}] -->
    <update id="update">
        UPDATE  ${table.name} set <#noparse>${</#noparse>ew.sqlSet<#noparse>}</#noparse>
        <if test="ew != null and ew.customSqlSegment != null">
            <include refid="Where_${entity}"></include>
        </if>
    </update>


    <!-- [删除${cfg.controllerName}] -->
    <delete id="removeById" parameterType="${package.Entity}.${entity}">
        UPDATE  ${table.name}
        <set>
            is_del= '1',
            <if test="deleteBy != null">
                delete_by = <#noparse>#{deleteBy},</#noparse>
            </if>
            <if test="deleteName != null">
                delete_name = <#noparse>#{deleteName},</#noparse>
            </if>
            <if test="deleteTime != null">
                delete_time = <#noparse>#{deleteTime},</#noparse>
            </if>
        </set>
        where
        id = <#noparse>#{id}</#noparse>
    </delete>


    <!-- [编辑${entity}] -->
    <update id="updateById" parameterType="${package.Entity}.${entity}">
        UPDATE  ${table.name}
        <set>
        <#list table.commonFields as field><#--生成公共字段 -->
            <if test="${field.propertyName} != null">
                ${field.name}=<#noparse>#{</#noparse>${field.propertyName}<#noparse>},</#noparse>
            </if>
        </#list>
    <#list table.fields as field>
        <#if !field.keyFlag><#--生成普通字段 -->
            <if test="${field.propertyName} != null">
                ${field.name}=<#noparse>#{</#noparse>${field.propertyName}<#noparse>},</#noparse>
            </if>
        </#if>
    </#list>
        </set>
        where id = <#noparse>#{id}</#noparse>
    </update>
</mapper>
